package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EnchantAccessory {
  private static Logger _log = Logger.getLogger(EnchantAccessory.class.getName());
  
  private static final Log _logx = LogFactory.getLog(EnchantAccessory.class.getName());
  
  private static EnchantAccessory _instance;
  
  private final HashMap<Integer, L1WilliamEnchantAccessory> _ArmorIndex;
  
  public static EnchantAccessory getInstance() {
    if (_instance == null)
      _instance = new EnchantAccessory(); 
    return _instance;
  }
  
  private EnchantAccessory() {
    this._ArmorIndex = new HashMap<>();
    loadArmorOrginal();
  }
  
  private void loadArmorOrginal() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM w_饰品等级");
      rs = pstm.executeQuery();
      fillWeaponSkill(rs);
    } catch (SQLException e) {
      _log.log(Level.SEVERE, "error while creating w_饰品等级 table", e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  private void fillWeaponSkill(ResultSet rs) throws SQLException {
    PerformanceTimer timer = new PerformanceTimer();
    int id = 0;
    while (rs.next()) {
      int type = rs.getInt("type");
      int strength = rs.getInt("strength");
      int level = rs.getInt("level");
      byte addStr = rs.getByte("addStr");
      byte addDex = rs.getByte("addDex");
      byte addCon = rs.getByte("addCon");
      byte addInt = rs.getByte("addInt");
      byte addWis = rs.getByte("addWis");
      byte addCha = rs.getByte("addCha");
      int addAc = rs.getInt("addAc");
      int addMaxHp = rs.getInt("addMaxHp");
      int addMaxMp = rs.getInt("addMaxMp");
      int addHpr = rs.getInt("addHpr");
      int addMpr = rs.getInt("addMpr");
      int addDmg = rs.getInt("addDmg");
      int addBowDmg = rs.getInt("addBowDmg");
      int addHit = rs.getInt("addHit");
      int addBowHit = rs.getInt("addBowHit");
      int addDmgReduction = rs.getInt("addDmgReduction");
      int addMr = rs.getInt("addMr");
      int addSp = rs.getInt("addSp");
      int PVPdmg = rs.getInt("PVPdmg");
      int PVPdmgReduction = rs.getInt("PVPdmgReduction");
      int Potion_Heal = rs.getInt("Potion_Heal");
      int Potion_Healling = rs.getInt("Potion_Healling");
      int magic_hit = rs.getInt("add_magic_hit");
      L1WilliamEnchantAccessory ArmorOrginal = new L1WilliamEnchantAccessory(id, type, strength, level, 
          addStr, addDex, addCon, addInt, addWis, addCha, addAc, addMaxHp, addMaxMp, addHpr, addMpr, addDmg, 
          addBowDmg, addHit, addBowHit, addDmgReduction, addMr, addSp, PVPdmg, PVPdmgReduction, Potion_Heal, 
          Potion_Healling, magic_hit);
      this._ArmorIndex.put(Integer.valueOf(id), ArmorOrginal);
      id++;
    } 
    _logx.info("载入w_饰品等级数据资料数量: " + this._ArmorIndex.size() + "(" + timer.get() + "ms)");
  }
  
  public L1WilliamEnchantAccessory getTemplate(int Armor) {
    return this._ArmorIndex.get(Integer.valueOf(Armor));
  }
  
  public L1WilliamEnchantAccessory[] getArmorList() {
    return (L1WilliamEnchantAccessory[])this._ArmorIndex.values().toArray((Object[])new L1WilliamEnchantAccessory[this._ArmorIndex.size()]);
  }
}
